package com.topcoder.srm515;

import java.util.StringTokenizer;

public class NewItemShopTwo {
	public double getMaximum(String[] customers) {
		Item[] c1 = parse(customers[0]);
		Item[] c2 = parse(customers[1]);
		int p1 = 100;
		int p2 = 100;
		double sum1=0.0, sum2=0.0, sum=0.0;
		
		for (int i=0; i<24; i++) {
			p1 -= c1[i].p;
			p2 -= c2[i].p;
		}
		
		for (int i=23; i>=0; i--) {
			p1 += c1[i].p;
			p2 += c2[i].p;
			double pr1 = p1 == 0? 0 : 1.0 * c1[i].p / p1;
			double pr2 = p2 == 0? 0 : 1.0 * c2[i].p / p2;
			sum = pr1 * pr2 * Math.max(c1[i].c, c2[i].c) + 
					pr1 * (1 - pr2) * Math.max(c1[i].c, sum2) + 
					pr2 * (1 - pr1) * Math.max(c2[i].c, sum1) +
					(1 - pr1) * (1 - pr2) * sum;
			sum1 = pr1 * c1[i].c + (1 - pr1) * sum1;
			sum2 = pr2 * c2[i].c + (1 - pr2) * sum2;
		}
		
		return sum;
	}
	
	private Item[] parse(String customer) {
		Item[] cs = new Item[24];
		for (int i=0; i<24; i++) cs[i] = new Item(0, 0);
		StringTokenizer st = new StringTokenizer(customer, ", ");
		while (st.hasMoreElements()) {
			int t = Integer.parseInt((String)st.nextElement());
			int c = Integer.parseInt((String)st.nextElement());
			int p = Integer.parseInt((String)st.nextElement());
			cs[t] = new Item(c, p);
		}
		return cs;
	}
	
	private class Item {
		int c;
		int p;
		public Item(int c, int p) {
			this.c = c;
			this.p = p;
		}
	}
}
